package com.pubg.information.controller;

import com.pubg.information.bean.Members;
import com.pubg.information.service.PersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class FileController {
    @Autowired
    PersonalService personalService;

    @RequestMapping(value = "/file")
    public String file(){
        return "file";
    }

    @RequestMapping(value = "/file/upload")
    public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile file,
                                   Model model,
                                   HttpServletRequest request,
                                   HttpSession session) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }

        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "C:/img/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String account = (String) session.getAttribute("userAccount");
        String imgUrl = "/img/portrait/" + fileName;
        personalService.updImgUrl(imgUrl,account);
        ModelAndView modelAndView = new ModelAndView("personal");
        Members memb = personalService.getMembers(account);

        modelAndView.addObject("memb",memb);

        return modelAndView;
    }
    @RequestMapping(value = "/file/uploadPhoto")
    @ResponseBody
    public Map fileUpload(MultipartFile photo, HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (photo == null) {
            ret.put("type", "error");
            ret.put("msg", "选择要上传的文件！");
            return ret;
        }
        if (photo.getSize() > 1024 * 1024 * 10) {
            ret.put("type", "error");
            ret.put("msg", "文件大小不能超过10M！");
            return ret;
        }

        String fileName = photo.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "C:/img/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            photo.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            ret.put("type", "error");
            ret.put("msg", "保存文件异常！");
            return ret;
        }
        String imgUrl = "/img/portrait/" + fileName;
        ret.put("type", "success");
        ret.put("msg", "上传图片成功！");
        ret.put("imgUrl",imgUrl);
        return ret;
    }
    //上传视频
    @RequestMapping(value = "/file/uploadVideo")
    @ResponseBody
    public Map VideoUpload(MultipartFile video, HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<String, Object>();
        if (video == null) {
            ret.put("type", "error");
            ret.put("msg", "选择要上传的文件！");
            return ret;
        }
        if (video.getSize() > 1024 * 1024 * 100) {
            ret.put("type", "error");
            ret.put("msg", "文件大小不能超过100M！");
            return ret;
        }

        String fileName = video.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "C:/video/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            video.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            ret.put("type", "error");
            ret.put("msg", "保存文件异常！");
            return ret;
        }
        String videoUrl = "/video/" + fileName;
        ret.put("type", "success");
        ret.put("msg", "上传视频成功！");
        ret.put("videoUrl",videoUrl);
        return ret;
    }
}
